package com.lcm.openapi.common.models.base;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.baomidou.mybatisplus.annotation.FieldFill;
import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.annotation.TableField;
import com.baomidou.mybatisplus.annotation.TableId;
import lombok.Data;
import org.springframework.beans.BeanUtils;
import org.springframework.util.CollectionUtils;

import java.io.Serializable;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;

/**
 * @author lichuanming
 * 数据库基础对象
 */
@Data
public class BaseEntity implements Serializable {

    public final static String DEFAULT_USERNAME = "system";

    /**
     * 雪花字符型ID
     */
    @TableId(value = "id", type = IdType.ASSIGN_ID)
    private Long id;

    @TableField(fill = FieldFill.INSERT)
    private String createBy;

    @TableField(fill = FieldFill.INSERT)
    private LocalDateTime createTime;

    @TableField(fill = FieldFill.INSERT_UPDATE)
    private String updateBy;

    @TableField(fill = FieldFill.INSERT_UPDATE)
    private LocalDateTime updateTime;

    private Integer deleteFlag;

    public <T> T toVo(Class<T> clazz){
        T t = BeanUtils.instantiateClass(clazz);
        BeanUtils.copyProperties(this, t);
        return t;
    }

    public static <T> List<T> toVoList(List poList ,Class<T> clazz){
        if(CollectionUtils.isEmpty(poList)){
            return new ArrayList<>();
        }
        return JSONArray.parseArray(JSON.toJSONString(poList) ,clazz);
    }

}
